//
//  RegisterViewController.m
//  FoodCorner
//
//  Created by lanou3g on 16/3/8.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "RegisterViewController.h"
#import <AVOSCloud.h>
#import <Masonry.h>

#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height

@interface RegisterViewController ()<UITextFieldDelegate>
//设置注册页面的相关属性
@property (nonatomic ,strong)UITextField *emailTF;
@property (nonatomic ,strong)UITextField *userPwdTF;
@property (nonatomic ,strong)UITextField *confimPwdTF;
@property (nonatomic ,strong)UITextField *userTypeTF;
@property (nonatomic ,strong)UIImageView *iconView;
@property (nonatomic ,strong)UIPickerView *pickerV;

//注册按钮
@property (nonatomic ,strong)UIButton *but;




@end

@implementation RegisterViewController

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super init];
    if (self) {
       
    }
    return self;
}

- (void)addAllviews{
    
    self.emailTF = [[UITextField alloc]init];
    self.emailTF.placeholder = @"请输入邮箱";
    self.emailTF.layer.borderWidth = 0.5;
    self.emailTF.layer.cornerRadius = 5;
    self.emailTF.delegate = self;
    [self.view addSubview:self.emailTF];
    [self.emailTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kHeight *0.32);
        make.left.equalTo(self.view).offset(kWidth *0.05);
        make.size.mas_equalTo(CGSizeMake(kWidth *0.9,40));
    }];
    
    
    self.userPwdTF = [[UITextField alloc]init];
    self.userPwdTF.placeholder = @"请输入密码";
    self.userPwdTF.layer.borderWidth = 0.5;
    self.userPwdTF.layer.cornerRadius = 5;
    self.userPwdTF.delegate = self;
    self.userPwdTF.secureTextEntry = YES;
    [self.view addSubview:self.userPwdTF];
    [self.userPwdTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kHeight *0.4);
        make.left.equalTo(self.view).offset(kWidth *0.05);
        make.size.mas_equalTo(CGSizeMake(kWidth *0.9, 40));
    }];
    self.confimPwdTF = [[UITextField alloc]init];
    self.confimPwdTF.placeholder = @"请再次输入密码";
    self.confimPwdTF.layer.borderWidth = 0.5;
    self.confimPwdTF.layer.cornerRadius = 5;
    self.confimPwdTF.delegate = self;
    self.confimPwdTF.secureTextEntry = YES;
    [self.view addSubview:self.confimPwdTF];
    [self.confimPwdTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kHeight *0.48);
        make.left.equalTo(self.view).offset(kWidth *0.05);
        make.size.mas_equalTo(CGSizeMake(kWidth *0.9, 40));
    }];
    
    self.but = [[UIButton alloc]init];
    [self.but setTitle:@"注册" forState:(UIControlStateNormal)];
    [self.but addTarget:self action:@selector(didClickButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_but];
    [self.but mas_makeConstraints:^(MASConstraintMaker *make) {
       
        
    }];
    
    
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
//添加子视图
     [self addAllviews];
    self.navigationItem.title = @"注册";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(didClickLeftBtn:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStyleDone target:self action:@selector(didClickRightBtn:)];
    
}


//左边按钮点击事件
//- (void)didClickLeftBtn:(UIBarButtonItem *)sender
//{
//    [self.navigationController popViewControllerAnimated:YES];
//}

//右边按钮点击事件


- (void)didClickButton:(UIButton *)sender
{
    if (![self validateEmail:self.emailTF.text] ) {
        [self addAlert:@"请输入正确的邮箱"];
    }else if ([self.userPwdTF.text isEqualToString:@""]){
        
        [self addAlert:@"密码不能为空"];
    }else if (![self.userPwdTF.text isEqualToString:self.confimPwdTF.text]){
        
        [self addAlert:@"两次输入的密码不一致"];
    }else{
        
        
        //创建AVUser对象实例
        AVUser *user = [AVUser user];
        user.username = self.emailTF.text;
        user.password = self.userPwdTF.text;
        [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                //注册成功
                [self addAlert:@"注册成功"];
            }else{
                
                [self addAlert:@"注册失败，邮箱已经注册"];
            }
        }];
        
        
        
    }
 
    
}


//添加的警示弹框
- (void)addAlert:(NSString *)message
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"友情提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *isOK = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([message isEqualToString:@"注册成功"]) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }];
    [alert addAction:isOK];
    [self presentViewController:alert animated:YES completion:nil];
    
}


#pragma 触摸view收回键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
   
    [self.emailTF resignFirstResponder];
    [self.userPwdTF resignFirstResponder];
    [self.confimPwdTF resignFirstResponder];
    
    
    
}
#pragma mark 代理方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
    
}


#pragma mark 判断邮箱是否正确
/**
 *  判断输入的邮箱是否正确
 *
 *  @return YES/NO
 */

/**
 *  通过传的正则表达式来判断字符串是否相符
 *
 *  @param aRegex 匹配字符串
 *
 *  @return YES/NO
 */
//判断用户输入的邮箱地址是否合法
- (BOOL) validateEmail: (NSString *) candidate {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:candidate];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
